//
//  ApplyAuthBookTableViewCell.swift
//  Venus
//
//  Created by qurongxiang on 2018/8/4.
//  Copyright © 2018年 zhaowang. All rights reserved.
//

import UIKit
private let cellID = "cellID"

protocol  ApplyAuthBookTableViewCellDelegate : class {
    func clickApplyAuthBookIndex(index:Int)
    func clickApplyAuthBookDelIndex(index:Int)
}



class ApplyAuthBookTableViewCell: UITableViewCell {
    weak var delegate: ApplyAuthBookTableViewCellDelegate?
    
    fileprivate lazy var currentModel:ApplyModel =  ApplyModel()
    
    fileprivate lazy var collectionView : UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = self.bounds.size
        layout.minimumLineSpacing = 10
        layout.minimumInteritemSpacing = 0
        layout.scrollDirection = .horizontal
        layout.sectionInset = UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 10)
        let collectionView = UICollectionView(frame: self.bounds, collectionViewLayout: layout)
        collectionView.dataSource = self
        collectionView.delegate = self
        collectionView.register(ApplyAuthBookCollectionViewCell.self, forCellWithReuseIdentifier: cellID)
        collectionView.isPagingEnabled = false
        collectionView.bounces = false
        collectionView.scrollsToTop = false
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.backgroundColor = UIColor.white
        return collectionView
    }()
    
  
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
}

// MARK: - 交互
extension ApplyAuthBookTableViewCell: ApplyAuthBookCollectionViewCellDelegate {
    
    
    func clickAuthBookDelBtn(index: Int) {
        delegate?.clickApplyAuthBookDelIndex(index: index)
    }
    
    func config(model:ApplyModel , index:IndexPath) {
        currentModel = model
        self.collectionView.reloadData()
    }
}

// MARK: - 代理
extension ApplyAuthBookTableViewCell: UICollectionViewDelegate , UICollectionViewDataSource ,UICollectionViewDelegateFlowLayout {
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return currentModel.bookArr.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellID, for: indexPath) as! ApplyAuthBookCollectionViewCell
        cell.delegate = self
 
        cell.config(model: currentModel, index: indexPath)
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: 100, height: 100)
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if indexPath.item == currentModel.bookArr.count - 1 {
             delegate?.clickApplyAuthBookIndex(index: indexPath.item)
        }
       
    }
}

extension  ApplyAuthBookTableViewCell {
    
    func setupUI() {
        contentView.addSubview(collectionView)
        collectionView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
    }
}
